package com.imooc.user.controller;

import com.imooc.user.constant.CookieConstant;
import com.imooc.user.constant.RedisConstant;
import com.imooc.user.dataobject.UserInfo;
import com.imooc.user.enums.ResultEnum;
import com.imooc.user.enums.RoleEnum;
import com.imooc.user.service.UserService;
import com.imooc.user.utils.CookieUtil;
import com.imooc.user.utils.ResultUtil;
import com.imooc.user.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 买家登录
     * 【笔记】需要往cookie中写入东西，所以需要HttpServletResponse
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid, HttpServletResponse response){

        // 1. 把入参openid与数据中的数据进行对比看是否匹配
        UserInfo userInfo = userService.findByOpenId(openid);
        if (userInfo == null){
            return ResultUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2. 判断角色
        if (!RoleEnum.BUYER.getCode().equals(userInfo.getRole())){
            return ResultUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3. cookie中设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID, openid, CookieConstant.expire);

        return ResultUtil.success();
    }

    /**
     * 卖家登录
     * 【笔记】
     *      需要获取cookie，所以需要 HttpServletRequest
     *      需要写入cookie，所以需要 HttpServletResponse
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openid") String openid, HttpServletRequest request,
                           HttpServletResponse response){

        // 判断是否已经登录
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null && !StringUtils.isEmpty(redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())))){
            return ResultUtil.success();
        }

        // 1. 把入参openid与数据中的数据进行对比看是否匹配
        UserInfo userInfo = userService.findByOpenId(openid);
        if (userInfo == null){
            return ResultUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2. 判断角色
        if (!RoleEnum.SELLER.getCode().equals(userInfo.getRole())){
            return ResultUtil.error(ResultEnum.ROLE_ERROR);
        }


        // 3. redis设置 key=UUID, value=xyz
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.expire;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE, token),
                openid, expire,TimeUnit.SECONDS);

        // 4. cookie中设置openid=abc
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.expire);

        return ResultUtil.success();
    }
}
